{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1.应用场景及定义方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 应用场景\n",
    "# • 在实际开发中，对象 的 某些属性或方法 可能只希望 在对象的内部被使用，而\n",
    "# 不希望在外部被访问到\n",
    "# • 私有属性 就是 对象 不希望公开的 属性\n",
    "# • 私有方法 就是 对象 不希望公开的 方法\n",
    "# 定义方式\n",
    "# • 在 定义属性或方法时，在 属性名或者方法名前 增加 '两个下划线'，定义的就是私有 属性或方法"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小芳\n",
      "我的年龄是18\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'Woman' object has no attribute '__secret'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "Input \u001B[1;32mIn [3]\u001B[0m, in \u001B[0;36m<cell line: 17>\u001B[1;34m()\u001B[0m\n\u001B[0;32m     15\u001B[0m \u001B[38;5;66;03m# print(xiaofang.__age)\u001B[39;00m\n\u001B[0;32m     16\u001B[0m xiaofang\u001B[38;5;241m.\u001B[39mage()\n\u001B[1;32m---> 17\u001B[0m \u001B[43mxiaofang\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m__secret\u001B[49m()\n",
      "\u001B[1;31mAttributeError\u001B[0m: 'Woman' object has no attribute '__secret'"
     ]
    }
   ],
   "source": [
    "class Woman:\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        # 不要问女生的年龄\n",
    "        self.__age = 18\n",
    "\n",
    "    def age(self):\n",
    "        print(\"我的年龄是%d\" % self.__age)\n",
    "\n",
    "    def __secret(self):\n",
    "        print(\"我的年龄是%d\" % self.__age)\n",
    "\n",
    "\n",
    "xiaofang = Woman(\"小芳\")\n",
    "print(xiaofang.name)\n",
    "# print(xiaofang.__age)\n",
    "xiaofang.age()\n",
    "xiaofang.__secret()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.伪私有属性和私有方法"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "我的年龄是18\n"
     ]
    }
   ],
   "source": [
    "# Python 中，并没有 真正意义 的 私有\n",
    "# • 在给 属性、方法 命名时，实际是对 名称 做了一些特殊处理，使得外界无法访\n",
    "# 问到\n",
    "# • 处理方式：在 名称 前面加上 _类名 => _类名__名称\n",
    "# 私有属性，外部不能直接访问到\n",
    "print(xiaofang._Woman__age)\n",
    "xiaofang._Woman__secret()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}